<template>
    <div>
      <!-- 搜索结果页-头部导航 -->
      <div class="search-result-container">
        <!-- 点击实现后退效果 -->
        <van-nav-bar
          title="搜索结果"
          left-arrow
          @click-left="$router.go(-1)"
          fixed
        />
      </div>
      <div>
          <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
        offset="50"
      >
        <ArticleItem v-for="itm in List" :key="itm.id" :arr="itm" :isShow='false' />
          </van-list>
      </div>
    </div>
</template>

<script>
import { searchResultListAPI } from '@/api/home'
export default {
  name: 'SearchResult',
  data () {
    return {
      List: [],
      loading: false, // 加载状态
      finished: false,
      page: 1
    }
  },
  computed: {
    txt () {
      return this.$route.params.kw
    }
  },
  created () {
    this.getSearchResultFn()
  },
  methods: {
    async getSearchResultFn () {
      const res = await searchResultListAPI({
        q: this.txt,
        page: this.page++
      })
      const { results } = res.data
      if (results.length < 10) {
        this.finished = true
        return
      }
      this.List = [...this.List, ...results]
      this.loading = false
    },
    onLoad () {
      if (this.List.length > 0) {
        this.getSearchResultFn()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
